/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Attributes.LibraryMetadataExtract;
import Records.RecordsMain;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Alexander
 */
//http://developer.echonest.com/api/v4/song/search?api_key=LXSEHQYDQJBY8GYMO&format=xml&results=1&artist=usher&title=omg&bucket=audio_summary&bucket=song_hotttnesss&bucket=song_discovery&bucket=id:7digital-US&bucket=tracks&limit=true
public class SwitchPic extends Thread {

    private final String location;
    private String image;

    private static boolean running;
    String previousImage;

    public SwitchPic(String loc, String pic) {
        System.out.println("SwitchPic.SwitchPic(" + loc + ", " + pic + ")");
        location = loc;
        image = pic;
        previousImage = "";
    }

    public void close() {
        System.out.println("SwitchPic.close()");
        running = false;
    }

    public void run() {
        System.out.println("SwitchPic.run()");
        running = true;
        while (running == true) {
            if (!location.contains("Directory")) {
                if (location.contains("Billboard")) {
                    image = "Billboard-Icon.png";
                } else if (location.contains("Hypem")) {
                    image = "Hype-Machine-Icon.png";
                } else if (location.contains("Itunes")) {
                    image = "ITunes-Icon.png";
                } else if (location.contains("UK40")) {
                    image = "UK40-Icon.png";
                } else if (location.contains("Shazam")) {
                    image = "Shazam-Icon.png";
                }
                if (!previousImage.equals(image)) {
                    setImage(image);
                    previousImage = image;
                    if (!RecordsMain.getUI().itunesDownloadButton.isVisible()) {
                        RecordsMain.getUI().itunesDownloadButton.setVisible(false);
                        RecordsMain.getUI().amazonDownloadButton.setVisible(false);
                    }
                }
            } else {
                //if (!previousImage.equals(image)) {
                setImage(image);
                //previousImage = image;
                if (RecordsMain.getUI().itunesDownloadButton.isVisible()) {
                    RecordsMain.getUI().itunesDownloadButton.setVisible(false);
                    RecordsMain.getUI().amazonDownloadButton.setVisible(false);
                }
            }
            running = false;
        }
    }

    public static void setImage(String FilePath) {
        System.out.println("SwitchPic.SwitchPic(" + FilePath + ")");

        BufferedImage img;
        Image newimg;
        if (FilePath.contains(".mp3")) {
            try {
                Mp3File song = new Mp3File(FilePath);
                if (song.hasId3v2Tag()) {
                    ID3v2 id3v2tag = song.getId3v2Tag();
                    byte[] imData = id3v2tag.getAlbumImage();
                    if (imData != null) {
                        img = ImageIO.read(new ByteArrayInputStream(imData));
                        newimg = img.getScaledInstance(174, 174, java.awt.Image.SCALE_SMOOTH);
                        Icon images = new ImageIcon(newimg);
                        RecordsMain.getUI().albumCover.setIcon(images);
                    }
                }
            } catch (IOException | UnsupportedTagException | InvalidDataException ex) {
                Logger.getLogger(LibraryMetadataExtract.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                img = ImageIO.read(new File(FilePath));
                newimg = img.getScaledInstance(174, 174, java.awt.Image.SCALE_SMOOTH);
                Icon images = new ImageIcon(newimg);
                RecordsMain.getUI().albumCover.setIcon(images);
            } catch (MalformedURLException ex) {
                Logger.getLogger(LibraryMetadataExtract.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(LibraryMetadataExtract.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
